package org.example.datastructure.ch6;

import java.util.LinkedList;
import java.util.Queue;

//二叉树实现类
public class TreeNode<T> {
    T val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(T val) {
        this.val = val;
    }

    TreeNode(T val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
    void preOrder(TreeNode root){
        if(root==null) return;
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }
    void postOrder(TreeNode root){
        if(root==null) return;
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }
    void midOrder(TreeNode root){
        if(root==null) return;
        midOrder(root.left);
        System.out.print(root.val+" ");
        midOrder(root.right);
    }
    void levelOrder(TreeNode root){
        if(root==null) return;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        TreeNode tmp;
        while(!queue.isEmpty()){
            tmp = queue.poll();
            System.out.print(tmp.val+" ");
            if(tmp.left!=null) queue.offer(tmp.left);
            if(tmp.right!=null) queue.offer(tmp.right);
        }
    }
    int size(TreeNode root){
        if(root==null) return 0;
        return 1+size(root.left)+size(root.right);
    }
    int height(TreeNode root){
        if(root==null) return 0;
        return 1+Math.max(height(root.left),height(root.right));
    }
    void preOrder1(TreeNode root){

    }
}
